package sdfs.util;

import sdfs.filetree.DirNode;
import sdfs.filetree.Entry;
import sdfs.filetree.Node;

import java.util.Iterator;

/**
 * Description: Tool
 */
public class Tool {
    /**
     * sdfs://127.0.0.1:8888/foo/too/a.txt
     * get the sub content of specific uri:    entry ~=~ dir
     * return entryList of specific dirNode
     */
    public static DirNode cdDir(String fileUri, DirNode root){
        String []path = getUris(fileUri); // foo  too
        int level = 0;
        DirNode curDir = root;
        while(level < path.length)
        {
            boolean found = false;
            Iterator<Entry> iterator = curDir.iterator();
            while (iterator.hasNext())
            {
                Entry entry = iterator.next();
                Node node = entry.getNode();
                if (!node.getClass().equals(DirNode.class)) continue;// skip the file
                DirNode dirNode = (DirNode)node;

                if(path[level].equals(dirNode.getName())){ // find one dir
                    curDir = dirNode; //change current dir
                    found=true;
                    break; // next level
                }
            }
            if (!found)// not found , and now to create it
            {
                DirNode dirNode= new DirNode();
                dirNode.setName(path[level]);
                curDir.addEntry(new Entry(path[level],dirNode));
                curDir = dirNode; //change current dir
            }
            level++;//next dir
        }
        return curDir;
    }

    /**
     * check the dir if it is exist
     * @param fileUri dir
     * @param root the root dir
     * @return boolean
     */
    public static boolean checkDir(String fileUri,DirNode root){
        String []path = getUris(fileUri); // foo  too
        int level = 0;
        DirNode curDir = root;
        while(level < path.length)
        {
            boolean found = false;
            Iterator<Entry> iterator = curDir.iterator();
            while (iterator.hasNext())
            {
                Node node = iterator.next().getNode();
                if (!node.getClass().equals(DirNode.class))
                    continue;// skip the file
                DirNode dirNode = (DirNode)node;
                if(node.getName().equals(path[level])){ // find one dir
                    curDir = dirNode; //change current dir
                    found=true;
                    break; // next level
                }
            }
            if (!found) return false;// not found , and now to create it
            level++;//next dir
        }
        return true;
    }

    public static boolean isFileExist(String fileUri,DirNode root){
        String []path = getUris(fileUri); // foo  too
        int level = 0;
        DirNode curDir = root;
        while(level < path.length-1)
        {
            boolean found = false;
            Iterator<Entry> iterator = curDir.iterator();
            while (iterator.hasNext())
            {
                Node node = iterator.next().getNode();
                if (!node.getClass().equals(DirNode.class))
                    continue;// skip the file
                DirNode dirNode = (DirNode)node;
                if(path[level].equals(dirNode.getName())){ // find one dir
                    curDir = dirNode; //change current dir
                    found=true;
                    break; // next level
                }
            }
            if (found)
                level++;//next dir
            else return false;
        }

        return true;
    }

    /**
     * get the path , the first one is a entry in root
     * @param fileUri uri
     * @return dirs or file  without ip and port
     */
    private static String[] getUris(String fileUri) {
        int start = fileUri.lastIndexOf(":")+6;
        String uri = fileUri.substring(start);
        return uri.split("/");
    }

    public static void printDir(DirNode dirNode,String space){
        for (Entry entry : dirNode) {
            System.out.println(space+entry.getNode().getName());
            if (entry.getNode().getClass().equals(DirNode.class))
                printDir((DirNode) entry.getNode(),space+"\t");
        }
    }
}
